﻿#region using
using System;
using Microsoft.Xna.Framework;

#endregion


namespace AwesomeTanks.Sources.Shape
{
    /// <summary> 
    /// Represents a 2D circle. 
    /// </summary> 
    public class Circle
    {
        #region Fields
        /// <summary> 
        /// Center position of the circle. 
        /// </summary> 
        public Vector2 Center { get; set; }

        /// <summary> 
        /// Radius of the circle. 
        /// </summary> 
        public float Radius { get; set; }

        #endregion

        #region Constructors
        /// <summary> 
        /// Constructs a new circle. 
        /// </summary> 
        public Circle(Vector2 position, float radius)
        {
            this.Center = position;
            this.Radius = radius;
        }

        #endregion

        #region Methods
        /// <summary> 
        /// Determines if a circle intersects a rectangle. 
        /// </summary> 
        /// <returns>True if the circle and rectangle overlap. False otherwise.</returns> 
        public bool Intersectsrectangle(Rectangle? rectangle)
        {
            float circleDistanceX = Math.Abs(this.Center.X - rectangle.Value.Center.X);
            float circleDistanceY = Math.Abs(this.Center.Y - rectangle.Value.Center.Y);

            if (circleDistanceX > (rectangle.Value.Width / 2 + this.Radius)) { return false; }
            if (circleDistanceY > (rectangle.Value.Height / 2 + this.Radius)) { return false; }

            if (circleDistanceX <= (rectangle.Value.Width / 2)) { return true; }
            if (circleDistanceY <= (rectangle.Value.Height / 2)) { return true; }

            float cornerDistance_sq = (float)(Math.Pow(circleDistanceX - rectangle.Value.Width / 2, 2) +
                         Math.Pow(circleDistanceY - rectangle.Value.Height / 2, 2));

            return (cornerDistance_sq <= (Math.Pow(this.Radius, 2)));
        }

        /// <summary> 
        /// Determines if a circle intersects another one. 
        /// </summary> 
        /// <returns>True if the circles overlap. False otherwise.</returns> 
        public bool IntersectsCircle(Circle circle)
        {
            float circleDistanceX = Math.Abs(this.Center.X - circle.Center.X);
            float circleDistanceY = Math.Abs(this.Center.Y - circle.Center.Y);

            if (circleDistanceX <= (circle.Radius) && circleDistanceY <= (circle.Radius)) { return true; }

            return false;
        }

        /// <summary>
        /// Rotates the vector2.
        /// </summary>
        /// <param name="point">The point.</param>
        /// <param name="radians">The radians.</param>
        /// <param name="pivot">The pivot.</param>
        /// <returns></returns>
        public static Vector2 RotateVector2(Vector2 point, float radians, Vector2 pivot)
        {
            float cosRadians = (float)Math.Cos(radians);
            float sinRadians = (float)Math.Sin(radians);

            Vector2 translatedPoint = new Vector2();
            translatedPoint.X = point.X - pivot.X;
            translatedPoint.Y = point.Y - pivot.Y;

            Vector2 rotatedPoint = new Vector2();
            rotatedPoint.X = translatedPoint.X * cosRadians - translatedPoint.Y * sinRadians + pivot.X;
            rotatedPoint.Y = translatedPoint.X * sinRadians + translatedPoint.Y * cosRadians + pivot.Y;

            return rotatedPoint;
        }
        #endregion
    }
}
